using Commons.Utils;

namespace Commons.Models.Functions
{
	public class PiecewiseFunction : AbstractFunction<double, double>
	{
		private RangeDictionary<double, IFunction<double, double>> functions;

		public PiecewiseFunction(string variable)
			: base(variable)
		{
			functions = new RangeDictionary<double, IFunction<double, double>>(true);
		}

		public void AddPiece(double x, IFunction<double, double> function)
		{
			functions.Add(x, function);
		}

		public override double Eval(double x)
		{
			IFunction<double, double> function = functions.Get(x);

			return function != null ? function.Eval(x) : 0;
		}
	}
}

